﻿Imports System.IO

Public Class Profile
    Inherits System.Web.UI.Page
    Public dbmanager As DBManager = New DBManager()
    Private checkDisplay As Boolean
    Private Shared userID As Integer
    Private Shared imageID As Integer

    'To set masterpage 
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        'Check if user is null
        If (Session("user") IsNot Nothing) Then
            'Retrieve userAcc details from session
            Dim userLoggedIn As userAcc = Session("user")
            'Check if user is admin, assign masterpage respectively
            If userLoggedIn.isAdmin Then
                Me.MasterPageFile = "~/MasterPage/Admin.Master"
            Else
                Me.MasterPageFile = "~/MasterPage/User.Master"
            End If
        Else
            'For anonymous users
            Me.MasterPageFile = "~/MasterPage/User.Master"
            'Enable this line to prevent anonymous users from entering this page
            ' Response.Redirect("~/Login.aspx")
        End If

    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        
        If Not IsPostBack Then
            'Load
            Dim userID As String
            Dim display_name = Request.QueryString("id")
            If Request.QueryString("ID") IsNot Nothing Then
                userID = dbmanager.retrieveUserID(display_name)
            End If
            imageID = dbmanager.getUserImage(userID)
            If Not imageID = 0 Then
                If Not userID = 0 Then
                    ImgUser.ImageUrl = ("IHttpHandler.ashx?id=" + imageID.ToString)
                    FileUploadImage.Visible = False
                End If
            Else
                'Set default user image 
                If imageID = 0 Then
                    imageID = 3
                    ImgUser.ImageUrl = "IHttpHandler.ashx?id=" + imageID.ToString
                    FileUploadNew.Visible = False
                End If
            End If

            Try
                Dim userProfile As userAcc = dbmanager.retrieveUser(display_name)
                lblDisplayEmail.Text = userProfile.email
                tbxDisplayName.Text = userProfile.display_name
                lblDisplayGender.Text = userProfile.gender
                Try
                    tbxFirstName.Text = userProfile.first_name
                Catch ex As Exception
                    tbxFirstName.Text = ""
                End Try
                Try
                    tbxLastName.Text = userProfile.last_name
                Catch ex As Exception
                    tbxLastName.Text = ""
                End Try
                If userProfile.date_of_birth.ToString = "1/1/1753 AM 12:00:00" Then
                    tbxDOB.Text = ""
                Else
                    tbxDOB.Text = userProfile.date_of_birth.ToShortDateString
                End If
                Try
                    ddlCountry.Text = userProfile.country
                Catch ex As Exception
                    ddlCountry.Text = "Unselected"
                End Try
            Catch ex As Exception
                MsgBox("Please login first", MsgBoxStyle.OkOnly, "Please login")
                Response.Redirect("home.aspx")
            End Try
            btnConfirm.Visible = False
        End If



    End Sub

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
            Dim display_name = tbxDisplayName.Text
            If MsgBox("Are you sure you want to delete your account?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
                dbmanager.deleteUser(display_name)
            MsgBox("Account Deleted")
                Response.Redirect("home.aspx")
            End If
    End Sub

    Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
        tbxDisplayName.Enabled = True
        tbxPassword.Enabled = True
        tbxRetypePassword.Enabled = True
        tbxFirstName.Enabled = True
        tbxLastName.Enabled = True
        tbxDOB.Enabled = True
        ddlCountry.Enabled = True
        btnConfirm.Visible = True
        btnUpdate.Visible = False
        FileUploadImage.Enabled = True
        FileUploadNew.Enabled = True
    End Sub


    Protected Sub btnConfirm_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnConfirm.Click
        Dim userID As String
        Dim temp_date As Date
        hfUserID.Value = 0
        hfImageID.Value = 0
        Dim display_name = Request.QueryString("id")
        If Request.QueryString("ID") IsNot Nothing Then
            userID = dbmanager.retrieveUserID(display_name)
        End If
        If (FileUploadImage.HasFile) Then
            Dim imgLength As Integer = FileUploadImage.PostedFile.ContentLength
            Dim img As HttpPostedFile = FileUploadImage.PostedFile
            Dim imgName As String = Path.GetFileName(FileUploadImage.PostedFile.FileName)
            If imgName.Contains(".jpg") Or imgName.Contains(".gif") Or imgName.Contains(".png") Or imgName.Contains(".jpeg") Or imgName.Contains(".bmp") Then
                hfImageID.Value = dbmanager.addImage(imgName, img, imgLength).ToString()
            Else
                MsgBox("Please enter your image in the format of jpg, gif, png, jpeg or bmp")
            End If
        End If
        If (FileUploadNew.HasFile) Then
            Dim imgLength As Integer = FileUploadNew.PostedFile.ContentLength
            Dim img As HttpPostedFile = FileUploadNew.PostedFile
            Dim imgName As String = Path.GetFileName(FileUploadNew.PostedFile.FileName)
            If imgName.Contains(".jpg") Or imgName.Contains(".gif") Or imgName.Contains(".png") Or imgName.Contains(".jpeg") Or imgName.Contains(".bmp") Then
                hfImageID.Value = dbmanager.updateImage(imageID, imgName, img, imgLength).ToString()
            Else
                MsgBox("Please enter your image in the format of jpg, gif, png, jpeg or bmp")
            End If
        End If
            If (tbxDisplayName.Text = display_name) Then
                Dim dob As String

            'If (tbxDOB.Text = "") Then
            '    dob = "1/1/1753 AM 12:00:00"
            'Else
            '    dob = tbxDOB.Text

            'End If

            If IsDate(tbxDOB.Text) = True Then
                dbmanager.updateUser(lblDisplayEmail.Text, tbxDisplayName.Text, tbxFirstName.Text, tbxLastName.Text, tbxDOB.Text, ddlCountry.Text)
                MsgBox("You have updated your profile!")
            Else
                IsDate(tbxDOB.Text)
                tbxDOB.Text = "1/1/1753 AM 12:00:00"
                'dob = tbxDOB.Text
                'dob = ""
                dbmanager.updateUser(lblDisplayEmail.Text, tbxDisplayName.Text, tbxFirstName.Text, tbxLastName.Text, tbxDOB.Text, ddlCountry.Text)
                MsgBox("You have updated your profile!")
            End If

        ElseIf (tbxDisplayName.Text.Length > 3) Then
            checkDisplay = dbmanager.checkAvailDisplayName(tbxDisplayName.Text)
            If checkDisplay = False Then
                lblWarningName.Visible = True
                lblWarningName.Text = "Display Name is not available"
                lblAvailName.Visible = False
            Else
                Try
                    dbmanager.updateUser(lblDisplayEmail.Text, tbxDisplayName.Text, tbxFirstName.Text, tbxLastName.Text, tbxDOB.Text, ddlCountry.Text)
                    MsgBox("You have updated your profile!")
                Catch ex As Exception
                    Try
                        dbmanager.updateUser(lblDisplayEmail.Text, tbxDisplayName.Text, tbxFirstName.Text, tbxLastName.Text, temp_date, ddlCountry.Text)
                        MsgBox("You have updated your profile!")
                    Catch ex2 As Exception
                        MsgBox("You have not updated your profile successfully")
                    End Try
                End Try
            End If
        End If

            tbxFirstName.Enabled = False
            tbxDisplayName.Enabled = False
            tbxPassword.Enabled = False
            tbxRetypePassword.Enabled = False
            tbxFirstName.Enabled = False
            tbxLastName.Enabled = False
            tbxDOB.Enabled = False
            ddlCountry.Enabled = False
            btnConfirm.Visible = False
            btnUpdate.Visible = True

            hfUserID.Value = userID
            imageID = Convert.ToInt32(hfImageID.Value)
            dbmanager.assignUserImage(userID, imageID)
    End Sub

    Protected Sub btnChange_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnChange.Click
        Try
            Dim hash = dbmanager.hashPassword(tbxPassword.Text)
            dbmanager.changePassword(tbxDisplayName.Text, hash)
            MsgBox("Your new password is changed")
        Catch ex As Exception
            MsgBox("Your password is not changed")
        End Try

    End Sub


    Protected Sub btnCheck_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCheck.Click
        If (tbxDisplayName.Text.Length > 3) Then
            checkDisplay = dbmanager.checkAvailDisplayName(tbxDisplayName.Text)
            If checkDisplay = False Then
                lblWarningName.Visible = True
                lblWarningName.Text = "Display Name is not available"
                lblAvailName.Visible = False
                tbxDisplayName.ReadOnly = False
            Else
                lblAvailName.Visible = True
                lblAvailName.Text = "Display Name is available"
                lblWarningName.Visible = False
                tbxDisplayName.ReadOnly = True
            End If
        Else
            lblWarningName.Visible = True
            lblWarningName.Text = "Display Name must be 4 or more charcters"
        End If
    End Sub

    Protected Sub btnImageDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnImageDelete.Click
        'imageID = dbmanager.getUserImage(userID)
        If Not imageID = 3 Then
        dbmanager.deleteImage(imageID)
        imageID = dbmanager.getUserImage(userID)
        If imageID = 0 Then
            imageID = 3
            ImgUser.ImageUrl = "IHttpHandler.ashx?id=" + imageID.ToString
            End If
        Else
            MsgBox("You are not able to delete the default image")
        End If
    End Sub
End Class